What is papaparse?
Papa Parse is a powerful, in-browser CSV (Comma Separated Values) parser which enables you to parse CSV files and strings. It can handle large files, and provides a variety of configuration options to customize the parsing process. It also supports converting JSON to CSV, making it a versatile tool for data processing in JavaScript applications.
What are papaparse's main functionalities?
CSV Parsing
This feature allows you to parse CSV strings. The 'complete' callback function is called after parsing is finished, and the 'results' object contains the parsed data.
Papa.parse(csvString, {
complete: function(results) {
console.log('Parsed results:', results.data);
}
});
CSV File Parsing
This feature is used to parse CSV files directly from a file input. It reads the file using the FileReader API and parses its contents.
Papa.parse(fileInput.files[0], {
complete: function(results) {
console.log('Parsed file results:', results.data);
}
});
Stream Parsing
Stream parsing allows you to parse CSV data row by row. The 'step' callback is called for each row parsed, which is useful for processing large files without loading the entire file into memory.
Papa.parse(csvString, {
step: function(row) {
console.log('Row:', row.data);
},
complete: function() {
console.log('All rows parsed!');
}
});
Unparsing (JSON to CSV)
Unparsing is the process of converting JSON data into a CSV string. This is useful for generating CSV files from JSON data structures.
var csv = Papa.unparse([{"column1": "value1", "column2": "value2"}]);
console.log(csv);
Other packages similar to papaparse
csv-parser
csv-parser is a Node.js module that transforms CSV into readable streams. It is fast and simple to use, but unlike Papa Parse, it is primarily designed for Node.js environments and does not work in the browser.
fast-csv
fast-csv is another Node.js library for parsing and formatting CSVs. It provides a similar set of features to Papa Parse, including support for streams and a flexible API, but it is also focused on Node.js rather than the browser.
csvtojson
csvtojson is a full-featured CSV parser library that converts CSV to JSON. One of its main differences from Papa Parse is that it returns a promise interface, which can be more convenient for asynchronous operations in modern JavaScript development.
Parse CSV with JavaScript
Papa Parse is the fastest in-browser CSV (or delimited text) parser for JavaScript. It is reliable and correct according to RFC 4180, and it comes with these features:
- Easy to use
- Parse CSV files directly (local or over the network)
- Fast mode
- Stream large files (even via HTTP)
- Reverse parsing (converts JSON to CSV)
- Auto-detect delimiter
- Worker threads to keep your web page reactive
- Header row support
- Pause, resume, abort
- Can convert numbers and booleans to their types
- Optional jQuery integration to get files from
<input type="file">
elements - One of the only parsers that correctly handles line-breaks and quotations
Papa Parse has no dependencies - not even jQuery.
Install
papaparse is available on npm. It
can be installed with the following command:
npm install papaparse
If you don't want to use npm, papaparse.min.js can be downloaded to your project source.
Usage
import Papa from 'papaparse';
Papa.parse(file, config);
const csv = Papa.unparse(data[, config]);
Homepage & Demo
To learn how to use Papa Parse:
The website is hosted on Github Pages. Its content is also included in the docs folder of this repository. If you want to contribute on it just clone the master of this repository and open a pull request.
Papa Parse for Node
Papa Parse can parse a Readable Stream instead of a File when used in Node.js environments (in addition to plain strings). In this mode, encoding
must, if specified, be a Node-supported character encoding. The Papa.LocalChunkSize
, Papa.RemoteChunkSize
, download
, withCredentials
and worker
config options are unavailable.
Papa Parse can also parse in a node streaming style which makes .pipe
available. Simply pipe the Readable Stream to the stream returned from Papa.parse(Papa.NODE_STREAM_INPUT, options)
. The Papa.LocalChunkSize
, Papa.RemoteChunkSize
, download
, withCredentials
, worker
, step
, and complete
config options are unavailable. To register a callback with the stream to process data, use the data
event like so: stream.on('data', callback)
and to signal the end of stream, use the 'end' event like so: stream.on('end', callback)
.
Get Started
For usage instructions, see the homepage and, for more detail, the documentation.
Tests
Papa Parse is under test. Download this repository, run npm install
, then npm test
to run the tests.
Contributing
To discuss a new feature or ask a question, open an issue. To fix a bug, submit a pull request to be credited with the contributors! Remember, a pull request, with test, is best. You may also discuss on Twitter with #PapaParse or directly to me, @mholt6.
If you contribute a patch, ensure the tests suite is running correctly. We run continuous integration on each pull request and will not accept a patch that breaks the tests.